<div class="left">
    <?php
    $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
	'id' => 'ad-form',
	'enableAjaxValidation' => false,
	'enableClientValidation' => true,
	'clientOptions' => array(
	    'validateOnSubmit' => true
	),
	'type' => 'horizontal',
	'htmlOptions' => array(
	    'enctype' => 'multipart/form-data'
	)
    ));
    ?>

    <div class="title">Post Supplies</div>

    <?php echo $form->errorSummary($model) ?>

    <div class="sign-1 in-div">
        <div>
            <h4>Your ad category</h4>
            <div class="clear"></div>
	    <?php echo $form->dropDownList($model, 'main_category', CHtml::listData(Category::getList(), 'category_id', 'name')); ?>

	    <label>Duration of post</label>
	    <label class="radio inline">
		<input type="radio" class="price" value="price_two_week" name="duration" checked="" /> 2 weeks
	    </label>
	    <label class="radio inline">
		<input type="radio" class="price" value="price_one_month" name="duration" /> 1 month
	    </label>

            <p class="show-price"><label>Price: $<span id="subPrice"></span></label></p>
        </div>
        <div class="otherCat">
            <h4>Post this Ad in another category. </h4>
            <div class="clear"></div>

	    <?php foreach (Category::getList() as $cat) : ?>
    	    <label class="checkbox">
    		<input type="checkbox" value="<?php echo $cat->category_id ?>" class="price" name="Supply[otherCategoryIds][]" <?php echo in_array($cat->category_id, $model->otherCategoryIds) ? 'checked' : '' ?> /><?php echo h($cat->name) ?>, Price: $<?php echo number_format($cat->price_addition) ?>
    	    </label>
	    <?php endforeach; ?>
	    <div>Price: <span id="total-price-cat"></span></div>
        </div>
        <div class="clear"></div>
        <div class="total-price">Total Price: $<span class="totalPrice"></span></div>

    </div>

    <div class="sign-1 in-div">
        <h4>Contact Informations</h4>
        <p><em>All contact information will not be posted and is used for communication between you and buyer.</em></p>
        <div>
            <label>Contact Name *</label>
	    <?php echo $form->textField($model, 'contact_name', array('maxlength' => 60)); ?>
        </div>
        <div>
            <label>Email *</label>
	    <?php echo $form->textField($model, 'contact_email', array('maxlength' => 60)); ?>
        </div>
        <div>
            <label>Country</label>
	    <?php echo $form->dropDownList($model, 'contact_country', CHtml::listData(Country::model()->findAll(array('order' => 'country_name')), 'country_iso', 'country_name'), array('maxlength' => 60)); ?>
        </div>
        <div>
            <label>State</label>
	    <?php echo $form->textField($model, 'contact_state', array('maxlength' => 60)); ?>
        </div>

        <div>
            <label>City</label>
	    <?php echo $form->textField($model, 'contact_city', array('maxlength' => 60)); ?>
        </div>
        <div>
            <label>Website</label>
	    <?php echo $form->textField($model, 'website', array('maxlength' => 255)); ?>
        </div>
    </div><!--in-div-->


    <div class="sign-1 in-div">
        <div>
            <h4>Type of Product</h4>
	    <?php echo $form->dropDownList($model, 'productType', Equipment::getTypes()) ?>
        </div>

        <div>
            <label>Manufacturer:</label>
	    <?php echo $form->dropDownList($model, 'manufacturer', Manufacturer::getArrayList()) ?>
        </div>
    </div><!--in-div-->

    <div class="sign-1 in-div">

        <div style="width:600px; float:none;">
            <label>Price</label>
            <label class="radio inline">
                <input type="radio" value="" name="price" class="price1" <?php if (!$model->price) echo 'checked' ?> /> All reasonable offers considered
            </label>
            <label class="radio inline">
                <input type="radio" value="1" name="price" class="price2" <?php if ($model->price > 0) echo 'checked' ?> /> Price
            </label>
            <span class="Show-in txt-price" <?php if (!$model->price) echo 'style="display:none;"' ?>>
		<?php echo $form->textField($model, 'price') ?>
            </span>
        </div>

        <div class="line-ke" ></div>
        <div>
            <label>Upload picture</label>
	    <?php echo $form->fileField($model, 'image') ?>
        </div>

    </div><!--in-div-->


    <div class="sign-1 in-div">
        <h4>Describe your ad</h4>
        <div>
            <label>Title<span class="star">*</span></label>
	    <?php echo $form->textField($model, 'title', array('maxlength' => 255, 'span6')); ?>
        </div>

        <div style="width:500px;">
            <label>Description<span class="star">*</span></label>
	    <?php echo $form->textArea($model, 'content', array('rows' => 10, 'cols' => 50, 'class' => 'span6')); ?>
        </div>
    </div>
    <div class="total-price">Total Price: $<span class="totalPrice"></span></div>
    <div class="sign-1 in-div">
        <button type="submit" class="btn btn-warning">Post Ads</button>
    </div>
    <?php $this->endWidget(); ?>
</div>

<?php $this->renderPartial('//ad/register/_right', array("type" => "supply")) ?>

<script>
    $(document).ready(function() {
	var cat = [];
<?php foreach (Category::getList() as $cat) : ?>
    <?php
    /*
      cat.push({
      two_week: <?php echo $cat->price_two_week ?>,
      one_month: <?php echo $cat->price_one_month ?>,
      addition: <?php echo $cat->price_addition ?>
      });
     * 
     */
    ?>
    	cat.push({
    	    price_two_week: <?php echo $cat->price_two_week ?>,
    	    price_one_month: <?php echo $cat->price_one_month ?>,
    	    price_3_month: <?php echo $cat->price_3_month ?>,
    	    price_6_month: <?php echo $cat->price_6_month ?>,
    	    price_one_year: <?php echo $cat->price_one_year ?>,
    	    price_addition: <?php echo $cat->price_addition ?>
    	});

<?php endforeach; ?>

	$('#Supply_main_category').change(function() {
	    var index = $(this).prop("selectedIndex");
	    //disable checkbox
	    $('.otherCat input[type=checkbox]').each(function(i) {
		if (i == index) {
		    $(this).attr('disabled', 'disabled');
		    $(this).attr('checked', false);
		} else {
		    $(this).removeAttr('disabled');
		}
	    }).promise().done(function() {
		calTotal();
	    });
	});
	$('input[name=duration]').change(function() {
	    calTotal();
	});
	$('input[type=checkbox].price').change(function() {
	    calTotal();
	});
	function calTotal() {
	    var type = $('input[name=duration]:checked').val();
	    //main cat
	    var index = $('#Supply_main_category').prop("selectedIndex");
	    var subPrice = cat[index][type];
	    //get checkbox
	    var otherCat = 0;
	    var numCatChose = 0;
	    var finalPrice = 0;
	    $('input[type=checkbox].price:checked').each(function() {
		//get index
		var index = $('.otherCat input[type=checkbox]').index($(this));
		otherCat += cat[index].price_addition;
		numCatChose += 1;
	    }).promise().done(function() {
		$('#subPrice').html(subPrice.toFixed(2));

		if (numCatChose > 1) {
		    var discount = <?php echo settings()->get('discount', 'discount_category') ?>;
		    finalPrice = otherCat - otherCat * (parseFloat(discount) / 100);
		    $('#total-price-cat').html("$" + finalPrice + '(discounted ' + discount + '%)');
		} else {
		    $('#total-price-cat').html("$" + otherCat);
		    finalPrice = otherCat;
		}

		$('.totalPrice').html((finalPrice + subPrice).toFixed(2));
	    });
	}

	//trigger
	$('#Supply_main_category').trigger('change');

	$('input[type="radio"]').click(function() {
	    var showPrice = $(this).val();
	    if (showPrice) {
		$('.txt-price').show();
	    } else {
		$('.txt-price').hide();
	    }
	});
    });
</script>